package com.red.star.macalline.data.repository;

import com.red.star.macalline.data.core.constant.MongoConstant;
import com.red.star.macalline.data.entity.ChatList;
import com.red.star.macalline.data.entity.ChatUser;
import com.red.star.macalline.data.utils.CheckUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/2/6.
 * 聊天列表数据库访问类
 */
@Repository
public class ChatListRepository {

    @Resource
    private MongoTemplate mongoTemplate;

    public List<ChatUser> findAllChatUserByFansId(Integer fansId) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        ChatList chatList = mongoTemplate.findOne(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST);
        if (chatList != null) {
            return chatList.getChatUsers();
        }
        return null;
    }

    public int addUserToChatList(Integer fansId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).push("chat_users", chatUser);
        int n = mongoTemplate.upsert(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    public int deleteUserFromChatList(Integer fansId, ChatUser chatUser) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).pull("chat_users", chatUser);
        int n = mongoTemplate.updateFirst(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }

    public int clearChatList(Integer fansId) {
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        return mongoTemplate.remove(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
    }

    /**
     * 查询三十分钟内活跃的用户
     *
     * @return
     */
    public List<ChatList> findActiveUserInThirtyMinute() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -30);
        Query query = Query.query(Criteria.where("update_time").gt(calendar.getTime()));
        return mongoTemplate.find(query, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST);
    }

    public int clearHomeUserList(Integer fansId, String mallCode) {
        ChatUser chatUser = new ChatUser();
        chatUser.setMallCode(mallCode);
        Query query = Query.query(Criteria.where("fans_id").is(fansId));
        Update update = Update.update("update_time", new Date()).pull("chat_users", chatUser);
        int n = mongoTemplate.updateMulti(query, update, ChatList.class, MongoConstant.COLLECTION_NAME_OF_CHAT_LIST).getN();
        return n;
    }
}
